package com.steak.orm.builder;

import com.steak.orm.annotation.PrimaryKey;
import com.steak.orm.annotation.Table;
import com.steak.orm.tool.StringUtil;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author 刘牌
 * @Title:
 * @date 2022/3/413:58
 */
public abstract class BaseSQLBuilder {

    protected String tableName; //表名
    protected String primaryKeyName; //主键名
    protected final String SELECT = "select ";
    protected final String FROM = " from ";
    protected final String WHERE = " where ";
    protected final String AND = " and ";
    protected final String IN = " IN ";
    protected final String UPDATE = " UPDATE ";
    protected final String SET = " SET ";
    protected final String VALUES = " VALUES ";
    protected final String OR = " OR ";
    protected final String DELETE = " DELETE ";
    protected final String INSERT = " INSERT ";
    protected final String INTO = " INTO ";

    protected StringBuilder sqlBuilder = new StringBuilder();

    protected void getTableName(Object obj){
        Table table = obj.getClass().getAnnotation(Table.class);
        tableName = table.value();
        if (Objects.equals(tableName, ""))
            tableName = StringUtil.getLastStr(obj.getClass().getName());
    }

    protected void getPrimaryKey(Field field){
        PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
        primaryKeyName = primaryKey.value();
        if (Objects.equals(primaryKeyName, ""))
            primaryKeyName = StringUtil.getLastStr(field.getClass().getName());
    }

    protected boolean hasPrimaryKey(Field field){
        PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
        return primaryKey != null;
    }

    protected String getField(String fieldStr){
        return "get" + fieldStr.substring(0, 1).toUpperCase() + fieldStr.substring(1);
    }

    protected Field[] getFields(Object obj){
        return obj.getClass().getDeclaredFields();
    }
}
